/*
 * CameraPreviewForm.cpp
 *
 *  Created on: Jun 24, 2011
 *      Author: MongKieu
 */
#include "../inc/Utility/MyParserXml.h"
#include "../inc/Ui/CameraPreviewForm.h"
#include "../inc/Ui/WebForm.h"
using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Graphics;
using namespace Osp::Io;
using namespace Osp::Media;
using namespace Osp::App;

CameraForm::CameraForm(void) {
	__pCameraPreview = null;
	__pOverlayCanvas = null;
	__pOverlayPanel = null;
	__pBackButton = null;
	__orientation = ORIENTATION_NONE;
	__isStarted = true;
	__pFrame = null;
	__pParentForm = null;
	__oPanelWidth = 0;
	__oPanelHeight = 0;
}

CameraForm::~CameraForm(void) {
	if (__pCameraPreview) {
		delete __pCameraPreview;
		__pCameraPreview = null;
	}

	if (__pOverlayCanvas) {
		delete __pOverlayCanvas;
		__pOverlayCanvas = null;
	}
}

result CameraForm::Construct(Frame *pFrame, Form *pMainForm,
		Orientation orientation) {
	result r = E_SUCCESS;

	r = Form::Construct(FORM_STYLE_NORMAL);
	SetName(L"CameraPreview");
	__pFrame = pFrame;
	__pParentForm = pMainForm;
	__orientation = orientation;

	return E_SUCCESS;
}

result CameraForm::OnInitializing(void) {
	result r = E_SUCCESS;
	r = __InitCamera();
	if (r != E_SUCCESS) {
		return r;
	}
	SetOrientation(__orientation);
	__InitOverlayPanel(__orientation);
	CameraForm::Start();
	return E_SUCCESS;
}

result CameraForm::OnTerminating(void) {
	__TerminateCamera();

	return E_SUCCESS;
}

void CameraForm::OnActionPerformed(const Control& source, int actionId) {
	result r = E_SUCCESS;

	if (actionId == IDC_GOTO_MAIN) {
		r = Cancel();
		_GotoMainForm();
	}
}
bool CameraForm::IsStarted(void) {
	return __isStarted;
}
result CameraForm::OnDraw() {
	result r = E_SUCCESS;

	r = Form::OnDraw();
	if (IsFailed(r)) {
		AppLog("OnDraw failed..");
		return r;
	}

	return r;
}

result CameraForm::Start(void) {
	result r = E_SUCCESS;
	Osp::Graphics::BufferInfo bufferInfo;
	r = __pCameraPreview->Initialize();
	__pOverlayPanel->GetBackgroundBufferInfo(bufferInfo);
	__pCameraPreview->StartPreview(&bufferInfo);
	return r;
}

result CameraForm::Stop() {
	result r = E_SUCCESS;

	r = __pCameraPreview->StopPreview();
	if (r == E_SUCCESS)
		__isStarted = false;

	return r;
}

result CameraForm::Cancel() {
	result r = CameraForm::Stop();
	if (r == E_SUCCESS)
		__pCameraPreview->Terminate();
	return r;
}

result CameraForm::InitButtons(Orientation orientation) {
	result r = E_SUCCESS;
	Rectangle backButtonRect;
	Rectangle diaDiemRect;
	Rectangle thoiTietLabelRect;
	if (orientation == ORIENTATION_PORTRAIT || orientation
			== ORIENTATION_PORTRAIT_REVERSE || orientation == ORIENTATION_NONE) {
		backButtonRect = Rectangle(320, 10, 150, 40);
		diaDiemRect = Rectangle(250, 125, 150, 150);
		thoiTietLabelRect = Rectangle(10, 125, 150, 150);

	} else {
		AppLog("000000000000000000000000000000000");
		backButtonRect = Rectangle(630, 10, 150, 40);
		diaDiemRect = Rectangle(270, 310, 550, 100);
		thoiTietLabelRect = Rectangle(270, 340, 550, 200);
	}
	__pBackButton = new Button();
	__pBackButton->Construct(backButtonRect, L"BACK");
	__pBackButton->SetActionId(IDC_GOTO_MAIN);
	__pBackButton->AddActionEventListener(*this);

	__pDiaDiem=new Label();
	__pDiaDiem->Construct(diaDiemRect,L"Dia Diem:");
	__pDiaDiem->SetTextConfig(30,LABEL_TEXT_STYLE_BOLD);
	__pDiaDiem->SetTextColor(Color::COLOR_WHITE);
	__pDiaDiem->SetTextHorizontalAlignment(ALIGNMENT_LEFT);

	__pThoiTiet=new Label();
	__pThoiTiet->Construct(thoiTietLabelRect,L"Thoi tiet:");
	__pThoiTiet->SetTextConfig(27,LABEL_TEXT_STYLE_ITALIC);
	__pThoiTiet->SetTextColor(Color::COLOR_CYAN);

	__pOverlayPanel->AddControl(*__pBackButton);
	__pOverlayPanel->AddControl(*__pDiaDiem);
	__pOverlayPanel->AddControl(*__pThoiTiet);

	__pBackButton->Draw();
	__pDiaDiem->Draw();
	__pThoiTiet->Draw();

	//String addressWeb="http://maps.google.com/maps/geo?q=10.7628273,106.6825514&output=xml";
	//Downloader::downloadAndSave(addressWeb,L"/Home/CameraDiaDiemGeo.xml", this);
	return r;
}

void CameraForm::_GotoMainForm() {
	__pCameraPreview->Terminate();
	__pFrame->SetCurrentForm(*__pParentForm);
	__pFrame->Draw();
	__pFrame->Show();
}
result CameraForm::__InitCamera(void) {
	__pCameraPreview = new CameraPreview();
	result r = E_SUCCESS;
	r = __pCameraPreview->Construct(*this);
	r = __pCameraPreview->PowerOn();
	return E_SUCCESS;
}

result CameraForm::__InitOverlayPanel(Orientation orientation) {
	result r = E_SUCCESS;
	if (orientation == ORIENTATION_PORTRAIT || orientation
			== ORIENTATION_PORTRAIT_REVERSE || orientation == ORIENTATION_NONE) {
		__oPanelWidth = 480;
		__oPanelHeight = 320;
	} else {
		__oPanelWidth = 800;
		__oPanelHeight = 480;
	}
	__pOverlayPanel = new OverlayPanel();
	r = __pOverlayPanel->Construct(Rectangle(0, 0, __oPanelWidth,
			__oPanelHeight));
	r = AddControl(*__pOverlayPanel);
	//  Get the OverlayCanvas
	__pOverlayCanvas = __pOverlayPanel->GetCanvasN();
	__pOverlayCanvas->Clear();

	return r;

}

result CameraForm::__TerminateCamera(void) {
	if (IsStarted())
		CameraForm::Stop();

	if (__pCameraPreview)
		__pCameraPreview->Terminate();

	return E_SUCCESS;
}

void CameraForm::OnCameraCaptured(Osp::Base::ByteBuffer& capturedData, result r) {
	AppLog("Vao Camera Captureed");
}

void CameraForm::OnCameraAutoFocused(bool completeCondition) {
	AppLog("OncameraAuto Focused");

}

void CameraForm::OnCameraPreviewed(Osp::Base::ByteBuffer& previewedData,
		result r) {
	AppLog("Thuc hien viec hien thi preview");
}

void CameraForm::OnCameraErrorOccurred(CameraErrorReason r) {
	AppLog("On Camera Occured");
}
void CameraForm::OnDownloadComplete(Osp::Base::String url,
		Osp::Base::String path) {

	String diaDiem = "/Home/CameraDiaDiemGeo.xml";
	String thoiTiet = "/Home/CameraThoiTiet.xml";
	if (path.Equals(diaDiem) == true) {
		AppLog("Path:%S",path.GetPointer());
		HienThiDiaDiemCameraVaDownloadThoiTiet();
	} else if (path.Equals(thoiTiet) == true) {
		AppLog("Download thanh cong thoi tiet");
		HienThiThoiTietCamera();
	}
}
void CameraForm::OnDownloadFailed(Osp::Base::String url, Osp::Base::String path) {
	AppLog("Thuc hein viec download that bai");
}
void CameraForm::HienThiDiaDiemCameraVaDownloadThoiTiet() {

	AppLog("Thuc hien viec download thanh cong");
	/* Ket thuc viec khai bao cho dien thoai*/
	MyParserXml *pMyParser = new MyParserXml();
	LinkedListT<DiaChi> *list = new LinkedListT<DiaChi> ();
	list->RemoveAll();

	list = pMyParser->getLayMangDiaChiBangTenDiaDiemHoacToaDo(
			"/Home/CameraDiaDiemGeo.xml");
	AppLog("Chieu dai:%d", list->GetCount());
	DiaChi p;
	list->GetAt(0, p);
	__pDiaDiem->SetText(p.tenDiaDiem);
	AppLog("%S",p.tenDiaDiem.GetPointer());
	__pDiaDiem->RequestRedraw(true);
	String addressWebThoiTiet =
			"www.google.com/ig/api?weather=ho chi minh , viet nam";
	Downloader::downloadAndSave(addressWebThoiTiet, L"/Home/CameraThoiTiet.xml", this);
	list->RemoveAll();
	delete list;
	delete pMyParser;
}
void CameraForm::HienThiThoiTietCamera() {
	AppLog("Trong day minh se thuc hien viec load du lieu thoi tiet");
	MyParserXml *pMyParserXml = new MyParserXml();
	MyCurrentWeather*pInfoWeather = new MyCurrentWeather();
	pInfoWeather = pMyParserXml->ThoiTietTrongNgay("/Home/CameraThoiTiet.xml");
	String pThongTin;
	pThongTin.Append(pInfoWeather->__myConditionCur);
	pThongTin.Append(pInfoWeather->__myWindCur);
	pThongTin.Append("\n");

	pThongTin.Append(pInfoWeather->__myTemp_cCur);
	pThongTin.Append(pInfoWeather->__myTemp_fCur);
	delete pMyParserXml;
	delete pInfoWeather;
	__pThoiTiet->SetText(pThongTin);
	__pThoiTiet->RequestRedraw(true);
}
void CameraForm::YeuCauCapNhatThongTinDiaDiemVaThoiTiet(float lat, float lng) {

	String addressWeb = "http://maps.google.com/maps/geo?q=";
	addressWeb.Append(lat);
	addressWeb.Append(",");
	addressWeb.Append(lng);
	addressWeb.Append("&output=xml");
	AppLog("Web:%S", addressWeb.GetPointer());
	Downloader::downloadAndSave(addressWeb, L"/Home/CameraDiaDiemGeo.xml", this);

}
